using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using GarageGames.Torque.Core;
using GarageGames.Torque.T3D;
using GarageGames.Torque.T3D.RigidCollision;
using GarageGames.Torque.Materials;

namespace TimeWar
{
    public class TimeWarGame
    {
        private int _friendlyIndex = 0;
        private int _enemyIndex = 0;
        private FriendlyTemplate _friendlyTemplate = null;
        private EnemyTemplate _enemyTemplate = null;
        //private TorqueObject _friendlyTemplate = null;
        //private TorqueObject _enemyTemplate = null;

        public TimeWarGame()
        {
        }

        public void CreateTemplates()
        {
            createEnemyTemplate();
            createFriendlyTemplate();
        }

        /// <summary>
        /// Generate the camera component for the game
        /// </summary>
        public void CreateCamera()
        {
            TorqueObject objCamera = new TorqueObject();
            objCamera.Name = "Time_War_Camera";

            T3DSceneComponent componentScene = new T3DSceneComponent();
            componentScene.SceneGroup = "Time_War_Camera";
            componentScene.Position = new Vector3(1024, 1024, 300);
            objCamera.Components.AddComponent(componentScene);

            T3DCameraComponent componentCamera = new T3DCameraComponent();
            componentCamera.SceneGroupName = "Time_War_Camera";
            objCamera.Components.AddComponent(componentCamera);

            TorqueObjectDatabase.Instance.Register(objCamera);
        }

        /// <summary>
        /// Create a point to spawn a character
        /// </summary>
        public void CreateSpawnPoint()
        {
            TorqueObject objSpawnPoint = new TorqueObject();
            objSpawnPoint.Name = "SpawnPoint";

            T3DSceneComponent componentScene = new T3DSceneComponent();
            componentScene.SceneGroup = "SpawnPoint";
            componentScene.Position = new Vector3(1024, 1024, 300);
            objSpawnPoint.Components.AddComponent(componentScene);

            T3DSpawnPointComponent componentSpawnPoint = new T3DSpawnPointComponent();
            componentSpawnPoint.SceneGroupName = "SpawnPoint";
            objSpawnPoint.Components.AddComponent(componentSpawnPoint);

            TorqueObjectDatabase.Instance.Register(objSpawnPoint);
        }

        /// <summary>
        /// Create a player character
        /// </summary>
        public void CreatePlayer(Vector3 position)
        {
            //find the physics resolver
            RigidCollisionManager rigidManager = TorqueObjectDatabase.Instance.FindObject<RigidCollisionManager>("RigidManager");

            TorqueObject objPlayer = new TorqueObject();
            objPlayer.Name = "PlayerObject";

            //give the player a shape
            T3DTSRenderComponent componentRender = new T3DTSRenderComponent();
            componentRender.SceneGroupName = "PlayerObject";
            componentRender.ShapeName = @"data\Characters\blue_player.dts";
            objPlayer.Components.AddComponent(componentRender);

            //give the player a presence within the scene
            T3DSceneComponent componentScene = new T3DSceneComponent();
            componentScene.SceneGroup = "PlayerObject";
            componentScene.Position = position;
            objPlayer.Components.AddComponent(componentScene);

            //give the player some physics attributes, like gravity and collision
            T3DRigidComponent componentPhysics = new T3DRigidComponent();
            componentPhysics.SceneGroupName = "PlayerObject";
            componentPhysics.GravityScale = 1.5f;//1.5f;
            componentPhysics.Mass = 10.0f;
            componentPhysics.RigidManager = rigidManager;
            componentPhysics.CollisionBody.AddCollisionShape(new CollisionBoxShape());
            objPlayer.Components.AddComponent(componentPhysics);

            //add the animation component
            T3DAnimationComponent componentAnimation = new T3DAnimationComponent();
            componentAnimation.SceneGroupName = "PlayerObject";
            objPlayer.Components.AddComponent(componentAnimation);

            //add the player movement component
            MovementComponent3D componentMovement = new MovementComponent3D();
            componentMovement.SceneGroupName = "PlayerObject";
            objPlayer.Components.AddComponent(componentMovement);

            //add a simple shadow component
            T3DBlobShadowCasterComponent componentShadow = new T3DBlobShadowCasterComponent();
            componentShadow.SceneGroupName = "PlayerObject";
            objPlayer.Components.AddComponent(componentShadow);

            //Mount the weapon
            T3DMountComponent componentMount = new T3DMountComponent();
            componentMount.SceneGroupName = "PlayerObject";
            objPlayer.Components.AddComponent(componentMount);

            WeaponComponent3D weaponRifle = new WeaponComponent3D();
            weaponRifle.Name = "WeaponRifle";
            objPlayer.Components.AddComponent(weaponRifle);

            TorqueObjectDatabase.Instance.Register(objPlayer);
        }

        /// <summary>
        /// Create a group of friendlies to help the player
        /// </summary>
        public void CreateFriendlies()
        {
            SpawnFriendly(new Vector3(990, 415, 400));
            SpawnFriendly(new Vector3(986, 415, 400));

        }

        /// <summary>
        /// Create a rifle to mount on the player
        /// </summary>
        protected void createRifle()
        {
            TorqueObject objRifle = new TorqueObject();
            T3DTSRenderComponent renderRifle = new T3DTSRenderComponent();
            renderRifle.SceneGroupName = "RifleObject";
            renderRifle.ShapeName = @"data\Weapons\prifle.dts";
            objRifle.Components.AddComponent(renderRifle);
            TorqueObjectDatabase.Instance.Register(objRifle);
        }

        /// <summary>
        /// Create and mount weapons (call after player created)
        /// </summary>
        public void CreateWeapons()
        {
            createRifle();

            FriendlyTemplate objPlayer = TorqueObjectDatabase.Instance.FindObject<FriendlyTemplate>("PlayerObject");
            TorqueObject objRifle = TorqueObjectDatabase.Instance.FindObject<TorqueObject>("RifleObject");
            if (objPlayer != null && objRifle != null)
            {
                T3DMountComponent componentMount = objPlayer.Components.FindComponent<T3DMountComponent>();
                T3DSceneComponent componentSceneChild = objRifle.Components.FindComponent<T3DSceneComponent>();
                componentMount.MountObject(componentSceneChild, "parentMountPoint0", "gunMountPoint0");
            }
        }

        protected void createEnemyTemplate()
        {
            _enemyTemplate = new EnemyTemplate();
            _enemyTemplate.SetName("EnemyTemplate");
            //add a new template to the TorqueObjectDatabase dictionary
            TorqueObjectDatabase.Instance.Dictionary.SetValue<TorqueObject>(_enemyTemplate, _enemyTemplate.Name, null);

            //TorqueObject enemyTemplate = new TorqueObject();

            ////find the physics resolver
            //RigidCollisionManager rigidManager = TorqueObjectDatabase.Instance.FindObject<RigidCollisionManager>("RigidManager");

            //enemyTemplate.Name = "EnemyTemplate";
            //enemyTemplate.IsTemplate = true;
            ////TemplateFlag = false;

            ////give the player a shape
            //T3DTSRenderComponent componentRender = new T3DTSRenderComponent();
            //componentRender.SceneGroupName = "EnemyTemplate";
            //componentRender.ShapeName = @"data\Characters\blue_player.dts";
            //enemyTemplate.Components.AddComponent(componentRender);

            ////give the player a presence within the scene
            //T3DSceneComponent componentScene = new T3DSceneComponent();
            //componentScene.SceneGroup = "EnemyTemplate";
            //componentScene.Position = new Vector3(1024, 1024, 300);
            //enemyTemplate.Components.AddComponent(componentScene);

            ////give the player some physics attributes, like gravity and collision
            ////CollisionSphereShape collisionSphere = new CollisionSphereShape();
            ////collisionSphere.Radius = 0.5f;
            //T3DRigidComponent componentPhysics = new T3DRigidComponent();
            //componentPhysics.SceneGroupName = "EnemyTemplate";
            //componentPhysics.GravityScale = 1.5f;
            //componentPhysics.Mass = 10.0f;
            //componentPhysics.RigidManager = rigidManager;
            ////componentPhysics.CollisionBody.AddCollisionShape(collisionSphere);
            //componentPhysics.CollisionBody.AddCollisionShape(new CollisionBoxShape());
            //enemyTemplate.Components.AddComponent(componentPhysics);

            ////add the animation component
            //T3DAnimationComponent componentAnimation = new T3DAnimationComponent();
            //componentAnimation.SceneGroupName = "EnemyTemplate";
            //enemyTemplate.Components.AddComponent(componentAnimation);

            ////add the AI component
            ////TorqueObject objPlayer = TorqueObjectDatabase.Instance.FindObject<TorqueObject>("PlayerObject");
            //AiComponent componentAI = new AiComponent("EnemyTemplate");
            ////componentAI.Friendlies.Add(objPlayer);
            //enemyTemplate.Components.AddComponent(componentAI);

            ////add a simple shadow component
            //T3DBlobShadowCasterComponent componentShadow = new T3DBlobShadowCasterComponent();
            //componentShadow.SceneGroupName = "EnemyTemplate";
            //enemyTemplate.Components.AddComponent(componentShadow);

            ////components so object can interact with other objects
            //CollisionSphereShape sphereCollision = new CollisionSphereShape();
            //sphereCollision.Radius = 5f;

            //T3DTriggerComponent componentTrigger = new T3DTriggerComponent();
            //componentTrigger.CollisionBody.AddCollisionShape(sphereCollision);
            //componentTrigger.OnCollision = OnInProximity;
            //componentTrigger.RigidManager = rigidManager;
            //componentTrigger.Immovable = false;
            //enemyTemplate.Components.AddComponent(componentTrigger);

            //T3DGroundControlComponent componentGroundControl = new T3DGroundControlComponent();
            //componentGroundControl.SceneGroupName = "EnemyTemplate";
            //componentGroundControl.ForceGravityOff = false;
            //enemyTemplate.Components.AddComponent(componentGroundControl);

            //TorqueObjectDatabase.Instance.Dictionary.SetValue<TorqueObject>(enemyTemplate, "EnemyTemplate", null);

            //_enemyTemplate = enemyTemplate;

        }

        public void SpawnEnemy(Vector3 position)
        {
            //TorqueObjectDatabase.Instance.Dictionary.GetValue<EnemyTemplate>(_enemyTemplate, "EnemyTemplate");
            //TorqueObject enemyTemplate = new TorqueObject();
            //TorqueObjectDatabase.Instance.Dictionary.GetValue<TorqueObject>(enemyTemplate, "EnemyTemplate");
            EnemyTemplate objEnemy = (EnemyTemplate)_enemyTemplate.Clone();
            //TorqueObject objEnemy = (TorqueObject)_enemyTemplate.Clone();
            //EnemyTemplate objEnemy = new EnemyTemplate();
            //objEnemy.TemplateFlag = false;
            objEnemy.IsTemplate = false;
            objEnemy.Components.FindComponent<T3DSceneComponent>().Position = position;
            string enemyName = "Enemy" + _enemyIndex;
            _enemyIndex++;
            objEnemy.SetName(enemyName);
            //objEnemy.Name = enemyName;
            //objEnemy.Components.FindComponent<T3DTSRenderComponent>().SceneGroupName = enemyName;
            //objEnemy.Components.FindComponent<T3DSceneComponent>().SceneGroup = enemyName;
            //objEnemy.Components.FindComponent<T3DRigidComponent>().SceneGroupName = enemyName;
            //objEnemy.Components.FindComponent<T3DAnimationComponent>().SceneGroupName = enemyName;
            //objEnemy.Components.FindComponent<AiComponent>().FriendlyName = enemyName;
            //objEnemy.Components.FindComponent<T3DBlobShadowCasterComponent>().SceneGroupName = enemyName;
            //objEnemy.Components.FindComponent<T3DTriggerComponent>().SceneGroupName = enemyName;
            //objEnemy.Components.FindComponent<T3DGroundControlComponent>().SceneGroupName = enemyName;
            TorqueObjectDatabase.Instance.Register(objEnemy);
        }

        protected void createFriendlyTemplate()
        {
            _friendlyTemplate = new FriendlyTemplate();
            _friendlyTemplate.SetName("FriendlyTemplate");
            TorqueObjectDatabase.Instance.Dictionary.SetValue<TorqueObject>(_friendlyTemplate, _friendlyTemplate.Name, null);

            //TorqueObject friendlyTemplate = new TorqueObject();

            ////find the physics resolver
            //RigidCollisionManager rigidManager = TorqueObjectDatabase.Instance.FindObject<RigidCollisionManager>("RigidManager");

            //friendlyTemplate.Name = "FriendlyTemplate";
            //friendlyTemplate.IsTemplate = true;
            ////TemplateFlag = false;

            ////give the player a shape
            //T3DTSRenderComponent componentRender = new T3DTSRenderComponent();
            //componentRender.SceneGroupName = "FriendlyTemplate";
            //componentRender.ShapeName = @"data\Characters\blue_player.dts";
            //friendlyTemplate.Components.AddComponent(componentRender);

            ////give the player a presence within the scene
            //T3DSceneComponent componentScene = new T3DSceneComponent();
            //componentScene.SceneGroup = "FriendlyTemplate";
            //componentScene.Position = new Vector3(1024, 1024, 300);
            //friendlyTemplate.Components.AddComponent(componentScene);

            ////give the player some physics attributes, like gravity and collision
            ////CollisionSphereShape collisionSphere = new CollisionSphereShape();
            ////collisionSphere.Radius = 0.5f;
            //T3DRigidComponent componentPhysics = new T3DRigidComponent();
            //componentPhysics.SceneGroupName = "FriendlyTemplate";
            //componentPhysics.GravityScale = 1.5f;
            //componentPhysics.Mass = 10.0f;
            //componentPhysics.RigidManager = rigidManager;
            ////componentPhysics.CollisionBody.AddCollisionShape(collisionSphere);
            //componentPhysics.CollisionBody.AddCollisionShape(new CollisionBoxShape());
            //friendlyTemplate.Components.AddComponent(componentPhysics);

            ////add the animation component
            //T3DAnimationComponent componentAnimation = new T3DAnimationComponent();
            //componentAnimation.SceneGroupName = "FriendlyTemplate";
            //friendlyTemplate.Components.AddComponent(componentAnimation);

            ////add the AI component
            ////TorqueObject objPlayer = TorqueObjectDatabase.Instance.FindObject<TorqueObject>("PlayerObject");
            //AiComponent componentAI = new AiComponent("FriendlyTemplate");
            ////componentAI.Friendlies.Add(objPlayer);
            //friendlyTemplate.Components.AddComponent(componentAI);

            ////add a simple shadow component
            //T3DBlobShadowCasterComponent componentShadow = new T3DBlobShadowCasterComponent();
            //componentShadow.SceneGroupName = "FriendlyTemplate";
            //friendlyTemplate.Components.AddComponent(componentShadow);

            ////components so object can interact with other objects
            //CollisionSphereShape sphereCollision = new CollisionSphereShape();
            //sphereCollision.Radius = 5f;

            //T3DTriggerComponent componentTrigger = new T3DTriggerComponent();
            //componentTrigger.CollisionBody.AddCollisionShape(sphereCollision);
            //componentTrigger.OnCollision = OnInProximity;
            //componentTrigger.RigidManager = rigidManager;
            //componentTrigger.Immovable = false;
            //friendlyTemplate.Components.AddComponent(componentTrigger);

            //T3DGroundControlComponent componentGroundControl = new T3DGroundControlComponent();
            //componentGroundControl.SceneGroupName = "FriendlyTemplate";
            //componentGroundControl.ForceGravityOff = false;
            //friendlyTemplate.Components.AddComponent(componentGroundControl);

            //TorqueObjectDatabase.Instance.Dictionary.SetValue<TorqueObject>(friendlyTemplate, "FriendlyTemplate", null);

            //_friendlyTemplate = friendlyTemplate;

        }

        public void SpawnFriendly(Vector3 position)
        {
            //TorqueObjectDatabase.Instance.Dictionary.GetValue<FriendlyTemplate>(_friendlyTemplate, "FriendlyTemplate");
            FriendlyTemplate objFriendly = (FriendlyTemplate)_friendlyTemplate.Clone();
            //TorqueObject friendlyTemplate = new TorqueObject();
            //TorqueObjectDatabase.Instance.Dictionary.GetValue<TorqueObject>(friendlyTemplate, "FriendlyTemplate", friendlyTemplate);
            //TorqueObject objFriendly = (TorqueObject)_friendlyTemplate.Clone();
            //FriendlyTemplate objFriendly = new FriendlyTemplate();
            objFriendly.IsTemplate = false;
            objFriendly.Components.FindComponent<T3DSceneComponent>().Position = position;
            string friendlyName = "FriendlyObject" + _friendlyIndex;
            _friendlyIndex++;
            objFriendly.SetName(friendlyName);
            //objFriendly.Name = friendlyName;
            //objFriendly.Components.FindComponent<T3DTSRenderComponent>().SceneGroupName = friendlyName;
            //objFriendly.Components.FindComponent<T3DSceneComponent>().SceneGroup = friendlyName;
            //objFriendly.Components.FindComponent<T3DRigidComponent>().SceneGroupName = friendlyName;
            //objFriendly.Components.FindComponent<T3DAnimationComponent>().SceneGroupName = friendlyName;
            //objFriendly.Components.FindComponent<AiComponent>().FriendlyName = friendlyName;
            //objFriendly.Components.FindComponent<T3DBlobShadowCasterComponent>().SceneGroupName = friendlyName;
            //objFriendly.Components.FindComponent<T3DTriggerComponent>().SceneGroupName = friendlyName;
            //objFriendly.Components.FindComponent<T3DGroundControlComponent>().SceneGroupName = friendlyName;

            //TorqueObject playerObject = TorqueObjectDatabase.Instance.FindObject<TorqueObject>("PlayerObject");
            //objFriendly.Components.FindComponent<AiComponent>().Friendlies.Add(playerObject);
            TorqueObjectDatabase.Instance.Register(objFriendly);
        }

        public void CreateEnemies()
        {
            SpawnEnemy(new Vector3(1000, 500, 400));
        }

        public void InitializeAI()
        {
            TorqueObject player = TorqueObjectDatabase.Instance.FindObject<TorqueObject>("PlayerObject");

            int i = 0;
            while (i < _friendlyIndex)
            {
                //FriendlyTemplate friendlyObject = (FriendlyTemplate)TorqueObjectDatabase.Instance.FindObject<TorqueObject>("Friendly" + i);
                TorqueObject friendlyObject = TorqueObjectDatabase.Instance.FindObject<TorqueObject>("Friendly" + i);
                if (friendlyObject.Components.FindComponent<AiComponent>().Friendlies == null)
                {
                    friendlyObject.Components.FindComponent<AiComponent>().Friendlies = new List<TorqueObject>();
                }

                friendlyObject.Components.FindComponent<AiComponent>().Friendlies.Add(player);
                friendlyObject.Components.FindComponent<AiComponent>().CurrentLeaderIndex = 0;
                int j = 0;
                while (j < _friendlyIndex)
                {
                    if (i != j)
                    {
                        //FriendlyTemplate anotherFriendly = (FriendlyTemplate)TorqueObjectDatabase.Instance.FindObject<TorqueObject>("Friendly" + j);
                        TorqueObject anotherFriendly = TorqueObjectDatabase.Instance.FindObject<TorqueObject>("Friendly" + j);
                        friendlyObject.Components.FindComponent<AiComponent>().Friendlies.Add(anotherFriendly);
                    }
                    j++;
                }

                j = 0;
                while (j < _enemyIndex)
                {
                    string enemyName = "Enemy" + j;
                    //EnemyTemplate enemy = (EnemyTemplate)TorqueObjectDatabase.Instance.FindObject<EnemyTemplate>(enemyName);
                    TorqueObject enemy = TorqueObjectDatabase.Instance.FindObject<TorqueObject>(enemyName);
                    if (enemy.Components.FindComponent<AiComponent>().Targets == null)
                    {
                        enemy.Components.FindComponent<AiComponent>().Targets = new List<TorqueObject>();
                    }
                    enemy.Components.FindComponent<AiComponent>().Targets.Add(friendlyObject);
                    if (friendlyObject != null)
                    {
                        friendlyObject.Components.FindComponent<AiComponent>().Targets.Add(enemy);
                    }
                    j++;
                }
                i++;
            }
            
            //Initialize AI components of enemies
            i = 0;
            while (i < _enemyIndex)
            {
                string enemyName = "Enemy" + i;
                //EnemyTemplate enemy = (EnemyTemplate)TorqueObjectDatabase.Instance.FindObject<EnemyTemplate>(enemyName);
                TorqueObject enemy = TorqueObjectDatabase.Instance.FindObject<TorqueObject>(enemyName);
                int j = 0;
                while (j < _enemyIndex)
                {
                    if (i != j)
                    {
                        enemyName = "Enemy" + j;
                        //EnemyTemplate anotherEnemy = (EnemyTemplate)TorqueObjectDatabase.Instance.FindObject<EnemyTemplate>(enemyName);
                        TorqueObject anotherEnemy = TorqueObjectDatabase.Instance.FindObject<TorqueObject>(enemyName);
                        enemy.Components.FindComponent<AiComponent>().Friendlies.Add(anotherEnemy);
                    }
                    j++;
                }

                //Add allies as targets
                if (enemy.Components.FindComponent<AiComponent>().Targets == null)
                {
                    enemy.Components.FindComponent<AiComponent>().Targets = new List<TorqueObject>();
                }
                enemy.Components.FindComponent<AiComponent>().Targets.Add(player);
                enemy.Components.FindComponent<AiComponent>().AiControlMode = AiMode.Chase;
                i++;
            }

        }

        /// <summary>
        /// Programmatically generates terrain instead of using Torque Scene loader
        /// </summary>
        public void CreateTerrain()
        {
            //find the physics resolver
            RigidCollisionManager rigidManager = TorqueObjectDatabase.Instance.FindObject<RigidCollisionManager>("RigidManager");

            //create the terrain
            XTerrain objTerrain = new XTerrain();
            objTerrain.Name = "Terrain";
            objTerrain.HorizontalScale = 8;
            objTerrain.VerticalScale = 1;
            objTerrain.LevelZeroError = 0;
            objTerrain.ViewError = 2;
            objTerrain.LODLevels = 4;
            objTerrain.LODError = 0.5f;
            objTerrain.Repeat = true;
            objTerrain.Visible = true;

            //give the terrain presence in the scene
            T3DSceneComponent componentScene = new T3DSceneComponent();
            componentScene.Position = new Vector3(0, 0, 0);
            componentScene.SceneGroup = "Terrain";
            objTerrain.Components.AddComponent(componentScene);

            RigidMaterial materialRigid = new RigidMaterial();
            materialRigid.Restitution = 0f;
            materialRigid.KineticFriction = 1f;
            materialRigid.StaticFriction = 1f;

            //give the terrain some physics attributes
            T3DRigidComponent componentPhysics = new T3DRigidComponent();
            componentPhysics.SceneGroupName = "Terrain";
            componentPhysics.GravityScale = 0f;
            componentPhysics.RigidManager = rigidManager;
            componentPhysics.RigidMaterial = materialRigid;
            componentPhysics.Immovable = true;
            CollisionXTerrainShape shape = new CollisionXTerrainShape();
            componentPhysics.CollisionBody.AddCollisionShape(shape);
            objTerrain.Components.AddComponent(componentPhysics);

            //Generate render material
            DetailMaterial sandDetail = new DetailMaterial();
            sandDetail.TextureFilename = "data/terrains/SandDetail01";
            sandDetail.DetailTextureRepeat = 256.0f;
            sandDetail.Name = "TerrainDetailMaterial";
            
            //specify a terrain data type
            TGETerrainData terrainData = new TGETerrainData();
            terrainData.TerrainFilename = "data/terrains/terrain.ter";
            terrainData.LightMapFilename = "data/terrains/lightmap.png";
            terrainData.TexturePathSubstitution = "data/terrains";
            terrainData.DetailMaterial = sandDetail;
            objTerrain.Data = terrainData;

            //register the terrain
            TorqueObjectDatabase.Instance.Register(objTerrain);
            //shape.GenerateCollisionData(objTerrain);
            shape.OnLoaded(objTerrain);
            componentPhysics.CollisionBody.UpdateShapes();
        }
        /// <summary>
        /// This method belongs in the Game.BeginRun() method.  All the initialization
        /// belongs here.
        /// </summary>
        public void BeginRun()
        {
            //Proper positions for allies
            //Player: 1000, 412, 300
            //Ally: 1210, 300, 300
            //Ally: 970, 200, 300

            //CreateTerrain();
            CreatePlayer(new Vector3(1000, 412, 300));
            //CreateTemplates();
            //CreateFriendlies();
            //CreateEnemies();
            CreateWeapons();
            //InitializeAI();
            //CreateSomething(new Vector3(988, 412, 410));
            CreateFriendly(_friendlyIndex, new Vector3(1210, 300, 300));
            _friendlyIndex++;
            CreateFriendly(_friendlyIndex, new Vector3(970, 200, 300));
            CreateEnemy(_enemyIndex, new Vector3(1000, 500, 1000));
            _enemyIndex++;
            InitializeAI();
        }

        public void OnInProximity(T3DRigidComponent me, T3DRigidComponent you, RigidContactConstraint.Contact[] contacts, int contactCount, float dt)
        {

        }

        /// <summary>
        /// Create test character
        /// </summary>
        /// <param name="position">Where on the map?</param>
        public void CreateSomething(Vector3 position)
        {
            //find the physics resolver
            RigidCollisionManager rigidManager = TorqueObjectDatabase.Instance.FindObject<RigidCollisionManager>("RigidManager");

            TorqueObject objPlayer = new TorqueObject();
            objPlayer.Name = "TestObject";

            //give the player a shape
            T3DTSRenderComponent componentRender = new T3DTSRenderComponent();
            componentRender.SceneGroupName = "TestObject";
            componentRender.ShapeName = @"data\Characters\blue_player.dts";
            objPlayer.Components.AddComponent(componentRender);

            //give the player a presence within the scene
            T3DSceneComponent componentScene = new T3DSceneComponent();
            componentScene.SceneGroup = "TestObject";
            componentScene.Position = position;
            objPlayer.Components.AddComponent(componentScene);

            //give the player some physics attributes, like gravity and collision
            T3DRigidComponent componentPhysics = new T3DRigidComponent();
            componentPhysics.SceneGroupName = "TestObject";
            componentPhysics.GravityScale = 10f;//1.5f;
            componentPhysics.Mass = 10.0f;
            componentPhysics.RigidManager = rigidManager;
            componentPhysics.CollisionBody.AddCollisionShape(new CollisionBoxShape());
            objPlayer.Components.AddComponent(componentPhysics);

            //add the animation component
            T3DAnimationComponent componentAnimation = new T3DAnimationComponent();
            componentAnimation.SceneGroupName = "TestObject";
            objPlayer.Components.AddComponent(componentAnimation);

            //add a simple shadow component
            T3DBlobShadowCasterComponent componentShadow = new T3DBlobShadowCasterComponent();
            componentShadow.SceneGroupName = "TestObject";
            objPlayer.Components.AddComponent(componentShadow);

            //Mount the weapon
            T3DMountComponent componentMount = new T3DMountComponent();
            componentMount.SceneGroupName = "TestObject";
            objPlayer.Components.AddComponent(componentMount);

            WeaponComponent3D weaponRifle = new WeaponComponent3D();
            weaponRifle.Name = "WeaponRifle";
            objPlayer.Components.AddComponent(weaponRifle);

            TorqueObjectDatabase.Instance.Register(objPlayer);
        }

        /// <summary>
        /// Create friendly character
        /// </summary>
        /// <param name="index">Friendly number</param>
        /// <param name="position">Where on the map?</param>
        public void CreateFriendly(int index, Vector3 position)
        {
            string objName = "Friendly" + index;

            //find the physics resolver
            RigidCollisionManager rigidManager = TorqueObjectDatabase.Instance.FindObject<RigidCollisionManager>("RigidManager");

            TorqueObject friendly = new TorqueObject();
            friendly.Name = objName;

            //give the player a shape
            T3DTSRenderComponent componentRender = new T3DTSRenderComponent();
            componentRender.SceneGroupName = objName;
            componentRender.ShapeName = @"data\Characters\blue_player.dts";
            friendly.Components.AddComponent(componentRender);

            //give the player a presence within the scene
            T3DSceneComponent componentScene = new T3DSceneComponent();
            componentScene.SceneGroup = objName;
            componentScene.Position = position;
            friendly.Components.AddComponent(componentScene);

            //Add the AI component
            AiComponent componentAI = new AiComponent(objName);
            componentAI.AiControlMode = AiMode.FollowLeader;
            friendly.Components.AddComponent(componentAI);

            //give the player some physics attributes, like gravity and collision
            T3DRigidComponent componentPhysics = new T3DRigidComponent();
            componentPhysics.SceneGroupName = objName;
            componentPhysics.GravityScale = 1.5f;//1.5f;
            componentPhysics.Mass = 10.0f;
            componentPhysics.RigidManager = rigidManager;
            componentPhysics.CollisionBody.AddCollisionShape(new CollisionBoxShape());
            friendly.Components.AddComponent(componentPhysics);

            //add the animation component
            T3DAnimationComponent componentAnimation = new T3DAnimationComponent();
            componentAnimation.SceneGroupName = objName;
            friendly.Components.AddComponent(componentAnimation);

            //add a simple shadow component
            T3DBlobShadowCasterComponent componentShadow = new T3DBlobShadowCasterComponent();
            componentShadow.SceneGroupName = objName;
            friendly.Components.AddComponent(componentShadow);

            //Mount the weapon
            T3DMountComponent componentMount = new T3DMountComponent();
            componentMount.SceneGroupName = objName;
            friendly.Components.AddComponent(componentMount);

            WeaponComponent3D weaponRifle = new WeaponComponent3D();
            weaponRifle.Name = "WeaponRifle";
            friendly.Components.AddComponent(weaponRifle);

            TorqueObjectDatabase.Instance.Register(friendly);
        }

        /// <summary>
        /// Create enemy character
        /// </summary>
        /// <param name="index">Enemy number</param>
        /// <param name="position">Where on the map?</param>
        public void CreateEnemy(int index, Vector3 position)
        {
            string objName = "Enemy" + index;

            //find the physics resolver
            RigidCollisionManager rigidManager = TorqueObjectDatabase.Instance.FindObject<RigidCollisionManager>("RigidManager");

            TorqueObject enemy = new TorqueObject();
            enemy.Name = objName;

            //give the player a shape
            T3DTSRenderComponent componentRender = new T3DTSRenderComponent();
            componentRender.SceneGroupName = objName;
            componentRender.ShapeName = @"data\Characters\blue_player.dts";
            enemy.Components.AddComponent(componentRender);

            //give the player a presence within the scene
            T3DSceneComponent componentScene = new T3DSceneComponent();
            componentScene.SceneGroup = objName;
            componentScene.Position = position;
            enemy.Components.AddComponent(componentScene);

            //give the player some physics attributes, like gravity and collision
            T3DRigidComponent componentPhysics = new T3DRigidComponent();
            componentPhysics.SceneGroupName = objName;
            componentPhysics.GravityScale = 1.5f;//1.5f;
            componentPhysics.Mass = 10.0f;
            componentPhysics.RigidManager = rigidManager;
            componentPhysics.CollisionBody.AddCollisionShape(new CollisionBoxShape());
            enemy.Components.AddComponent(componentPhysics);

            //add the animation component
            T3DAnimationComponent componentAnimation = new T3DAnimationComponent();
            componentAnimation.SceneGroupName = objName;
            enemy.Components.AddComponent(componentAnimation);

            //add a simple shadow component
            T3DBlobShadowCasterComponent componentShadow = new T3DBlobShadowCasterComponent();
            componentShadow.SceneGroupName = objName;
            enemy.Components.AddComponent(componentShadow);

            //Mount the weapon
            T3DMountComponent componentMount = new T3DMountComponent();
            componentMount.SceneGroupName = objName;
            enemy.Components.AddComponent(componentMount);

            WeaponComponent3D weaponRifle = new WeaponComponent3D();
            weaponRifle.Name = "WeaponRifle";
            enemy.Components.AddComponent(weaponRifle);

            //Add the AI component
            AiComponent componentAI = new AiComponent(objName);
            enemy.Components.AddComponent(componentAI);

            TorqueObjectDatabase.Instance.Register(enemy);
        }


    }
}
